<?php
/****************************************************************************

	COPYRIGHT(C) WWW.MAXSI.DK, MAXSI SOFTWARE, SORTIE 2009
	ALL RIGHTS RESERVED

	Maxsi Distribution ContentServer/ Branch
	Scripts running a Maxsi Distribution Compatible Content Server.	
	
	upload.php
	Uploads a single file to the content server.

****************************************************************************/

	// Todo! Make a system to log in to the content servers via the upload tools!

	//if (!$md_accountsys || !$md_account)
	//{
	//	// Todo: Security flaw.
	//	die('Failure!');
	//} 

	header('Transfer-Encoding: chunked');
	
	$BlockId			=	(int) md_RequestParam('blockid');
	$ContentArchive		=	md_RequestParam('contentarchive');
	
	if ( !isset($BlockId) )
	{
		mdc_SendErrorDie('Failure: Missing blockid entities.');
	}
	
	if ( !isset($ContentArchive) )
	{
		mdc_SendErrorDie('Failure: Missing contentarchive entities.');
	}
	
	if (!file_exists(md_ProductNameToFileName($ContentArchive)))
	{
		mkdir(md_ProductNameToFileName($ContentArchive),0700,true);
	}
	
	$Destination = md_ProductNameToFileName($ContentArchive) . '/' . md_ProductNameToFileName($ContentArchive) . '_Block_' . $BlockId . '.MaxsiDistribution';
	$TempFile = $_FILES['file']['tmp_name'];	
	if (!isset($_GET['append']))
	{
		copy($TempFile,$Destination);
	}
	else
	{
		// Split-Upload support. Just append the next potion of data!
		file_put_contents($Destination, file_get_contents($TempFile),FILE_APPEND);
	}
	// If everything went right, do output 'Success'
	echo 'Success';
?>